package org.bluedream.core.module.sys.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.bluedream.core.module.sys.entity.Menu;

import java.util.List;

public interface MenuDao extends BaseMapper<Menu> {
    @Select("select * from sys_menu where status = '0'")
    List<Menu> selectAll();

    @Select("select a.menu_name as treeName , a.* from sys_menu a order by ${orderByStr}")
    List<Menu> getAllMenus(@Param("orderByStr")String orderByStr);

    /**
     * 返回菜单明细，筛选必要条件：登录用户id ， 登录用户组织id
     * @param loginCode
     * @param orgCode
     * @param orderByStr 排序条件字符串
     * @return
     */
    @Select("select menu.menu_name as treeName , menu.* from sys_menu menu , sys_role_relation roleMenu , " +
            "        (select role.id as id  from sys_role role , sys_org_relation orgRelation , sys_organization org , sys_role_relation roleUser " +
            "            where orgRelation.status = '0' and org.status = '0' and role.status = '0' and roleUser.status = '0' " +
            "             and orgRelation.org_id = org.id and orgRelation.type = 'role' and orgRelation.entity_id = role.id " +
            "             and role.id = roleUser.role_id and roleUser.type = 'user' " +
            "             and roleUser.entity_id = #{login_Code} " +
            "             and org.id = #{loginOrg_id}) role " +
            "         where  menu.status = '0' and roleMenu.status = '0' " +
            "                and menu.id = roleMenu.entity_id and roleMenu.type = 'menu' " +
            "                and roleMenu.Role_Id = role.id " +
            "                order by ${orderByStr}")
    List<Menu> getMenusByLoginUserAndLoginOrg(@Param("login_Code")String loginCode , @Param("loginOrg_id")String orgCode , @Param("orderByStr")String orderByStr);

    /**
     * 返回菜单明细，筛选条件：角色id
     * @param roleId
     * @param orderByStr  排序条件字符串
     * @return
     */
    @Select("select menu.menu_name as treeName , menu.* " +
            "from sys_menu menu , sys_role_relation roleMenu " +
            "where menu.status = '0' and roleMenu.status = '0' " +
            "and menu.id = roleMenu.entity_id and roleMenu.type = 'menu' " +
            "and roleMenu.Role_Id = #{role_id} " +
            "order by ${orderByStr}")
    List<Menu> getMenusByRole(@Param("role_id")String roleId , @Param("orderByStr")String orderByStr);
}
